package com.example.mallsystem1104.controller;

import com.example.mallsystem1104.annotation.Authority;
import com.example.mallsystem1104.common.Result;
import com.example.mallsystem1104.entity.AuthorityType;
import com.example.mallsystem1104.entity.Good;
import com.example.mallsystem1104.service.GoodService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author zy
 * @Date 2024/12/10 17:53
 * @packageName:com.example.mallsystem1104.controller
 * @ClassName:GoodController
 */
@RestController
@RequestMapping("/api/good")
@CrossOrigin
public class GoodController {

    @Autowired
    private GoodService goodService;

    @GetMapping
    public Result findAll(){
        return Result.success(goodService.findAll());
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id){
        return Result.success(goodService.getGoodById(id));
    }

    @GetMapping("/standard/{id}")
    public Result findStandardById(@PathVariable Long id){
        return Result.success(goodService.getStandardById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(required = false,defaultValue = "1") Integer pageName,
                           @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                           @RequestParam(required = false,defaultValue = "") String searchText,
                           @RequestParam(required = false) Integer categoryId){
        return Result.success(goodService.findPage(pageName,pageSize,searchText,categoryId));
    }

    @PostMapping
    @Authority(AuthorityType.requireAuthority)
    public Result save(@RequestBody Good good){
        return Result.success(goodService.saveOrUpdateGood(good));
    }

    @GetMapping("/fullPage")
    public Result fullPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(required = false,defaultValue = "")String searchText,
            @RequestParam(required = false)Integer categoryId
    ){
        return Result.success(goodService.findFullPage(pageNum,pageSize,searchText,categoryId));
    }

    @GetMapping("/recommend")
    @Authority(AuthorityType.requireAuthority)
    public Result setRecommend(@RequestParam Long id, @RequestParam Boolean isRecommend){
        return Result.success(goodService.serRecommend(id,isRecommend));
    }

    @GetMapping("/rank")
    public Result getSaleRank(@RequestParam int num){
        return Result.success(goodService.getSaleRank(num));
    }


}
